layui.config({
        base: '/assets/module/'
}).extend({
    notice: 'notice/notice',
}).use(['layer', 'form', 'formX', 'setter', 'mynotice', 'cookie', 'admin', 'ax', 'util', 'notice'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var formX = layui.formX;
    var setter = layui.setter;
    var mynotice = layui.mynotice;
    var cookie = layui.cookie;
    var admin = layui.admin;
    var $ax = layui.ax;
    var util = layui.util;
    
    var UserLogin = {
    		pattern: /^1(3|4|5|7|8)\d{9}$/, // 手机号码校验正则表达式
    		username: "zp_username", // 登录账号
            password: "zp_password", //登录密码
            uuid: '', // 登录唯一标识
            reguuid: '', // 注册唯一标识
            gotuuid: '', // 找回密码唯一标识
            formatId: '', // 业态标签ID
    		setCookie: (data) => { // 设置Cookie
    			console.log(data);
                var remember = $("input[name='remember-me']").is(":checked"); // 是否记住密码
                var remember2 = $("input[name='remember-me2']").is(":checked"); // 是否记住密码
                if (remember || remember2) {// 如果选中-->记住密码登录
                    $.cookie(UserLogin.username, data.username.trim(), 7);//有效时间7天，也可以设置为永久，把时间去掉就好
                    $.cookie(UserLogin.password, data.password.trim(), 7);
                } else {// 如果没选中-->不记住密码登录
                    $.cookie(UserLogin.username, "");
                    $.cookie(UserLogin.password, "");
                }
            },
            getCookie: () => { // 获取Cookie
                var username = $.cookie(UserLogin.username); //获取cookie中的用户名    
                var password = $.cookie(UserLogin.password); //获取cookie中的登陆密码    
                if (password) {//密码存在的话把“记住用户名和密码”复选框勾选住    
                	$("input[name='remember-me']").attr('checked', true);
                	$("input[name='remember-me2']").attr('checked', true);
                    form.render('checkbox');
                }
                if (username != "") {//用户名存在的话把用户名填充到用户名文本框    
                    $("[name='username']").val(username);
                } else {
                    $("[name='username']").val("");
                }
                if (password != "") {//密码存在的话把密码填充到密码文本框    
                    $("[name='password']").val(password);
                } else {
                    $("[name='passWord']").val("");
                }
            },
            setCach: (data, remember) => { // 设置缓存
            	if(remember){
            		 setter.putAccountInfo(data);
            	}else{
            		setter.removeAccountInfo();
            	}
            },
            getCach: () => { // 获取缓存
            	var data = setter.getAccountInfo();
            	if(data){
            		if (data.username) {//密码存在的话把“记住用户名和密码”复选框勾选住    
                    	$("input[name='remember-me']").attr('checked', true);
                    	$("input[name='remember-me2']").attr('checked', true);
                        form.render('checkbox');
                        $("[name='username']").val(data.username);
                    }else{
                    	$("[name='username']").val("");
                    }
                    if (data.password) {//密码存在的话把密码填充到密码文本框    
                        $("[name='password']").val(data.password);
                    } else {
                        $("[name='passWord']").val("");
                    }
            	}else{
            		$("[name='username']").val("");
            		$("[name='passWord']").val("");
            	}
            }
    		
    }
    
    UserLogin.getCach();
    
    /**
     * 事件监听
     */
    util.event('lay-event', {
    	phoneCode: () => { // 手机验证码登录
    		$('#pcodeerror').hide();
        	$('#phoneSig-ccccc').hide();
            $("#passWhole").hide();
            $("#phoneWhole").show();
            $('#phonecode').val($('#a_username').val());
    	},
    	passSig: () => { // 密码登录
        	$('#account').hide();
        	$('#codeerror').hide();
        	$('#codeerror').hide();
            $("#passWhole").show();
            $("#phoneWhole").hide();
            $('#a_username').val($('#phonecode').val());
        },
        register: () => { // 去注册
        	$("#signupaccounterror").hide();
        	$("#signupcode").hide();
            $("#registerWhole").show();
            $("#passWhole").hide();
            $("#phoneWhole").hide();
            $("#signupaccounterror").hide();
            $("#signupcode").hide();
            $("#signinpwd").hide();
            $("#profession").hide();
            $('#regpassword').val('');
            
        },
        regSigIn: () => { // 去登录
        	$('#pcodeerror').hide();
        	$('#phoneSig-ccccc').hide();
            $("#registerWhole").hide();
            $("#passWhole").show();
            $("#phoneWhole").hide();
        },
        snedCode: () => { // 发送验证码
        	var phone = $('#phonecode').val();
        	if(phone == '' || phone == 0){
        		$("#phoneSig-ccccc").text('请输入手机号');
            	$("#phoneSig-ccccc").fadeIn();
            	return false;
        	}
        	if(!UserLogin.pattern.test(phone)){
        		$("#phoneSig-ccccc").text('手机号码格式不正确');
            	$("#phoneSig-ccccc").fadeIn();
            	return false;
        	}
        	$('#phoneSig-ccccc').hide();
        	admin.btnLoading('#snedCode', '&nbsp;验证码发送中'); 
        	var ajax = new $ax("/code/sms", 'get', function (res) {
        		UserLogin.uuid = res.data;
        		admin.btnLoading('#snedCode', false);
        		formX.startTimer('#snedCode', 30, function(time){
        			if(time == 1){
        				$('#snedCode').attr('disabled', false)
        			}
        			return time + 's 后可继续发送';
                });
            }, function (res) {
            	admin.btnLoading('#snedCode', false);
            	$('#snedCode').attr('disabled', false)
            	mynotice.error(res.msg);
            });
        	ajax.set('phone', phone);
            ajax.start();
        },
        regSnedCode: () => { // 注册发送短信验证码
        	var phone = $('#mobile').val();
        	if(phone == '' || phone == 0){
        		$("#signupaccounterror").text('请输入手机号');
            	$("#signupaccounterror").fadeIn();
            	return false;
        	}
        	if(!UserLogin.pattern.test(phone)){
        		$("#signupaccounterror").text('手机号码格式不正确');
            	$("#signupaccounterror").fadeIn();
            	return false;
        	}
        	$('#signupaccounterror').hide();
        	admin.btnLoading('#regSnedCode', '&nbsp;验证码发送中'); 
        	var ajax = new $ax("/register/sms", 'get', function (res) {
        		UserLogin.reguuid = res.data;
        		admin.btnLoading('#regSnedCode', false);
        		formX.startTimer('#regSnedCode', 30, function(time){
        			if(time == 1){
        				$('#regSnedCode').attr('disabled', false)
        			}
        			return time + 's 后可继续发送';
                });
            }, function (res) {
            	admin.btnLoading('#regSnedCode', false);
            	$('#regSnedCode').attr('disabled', false)
            	mynotice.error(res.msg);
            });
        	ajax.set('phone', phone);
            ajax.start();
        },
        newforgotCode: () => { // 找回密码发送短信验证码
        	var phone = $('#phonenumber').val();
        	if(phone == '' || phone == 0){
        		$("#phoneSig-ccccc").text('请输入手机号');
            	$("#phoneSig-ccccc").fadeIn();
            	return false;
        	}
        	if(!UserLogin.pattern.test(phone)){
        		$("#signupaccounterror").text('手机号码格式不正确');
            	$("#signupaccounterror").fadeIn();
            	return false;
        	}
        	$('#signupaccounterror').hide();
        	admin.btnLoading('#newForgotCode', '&nbsp;验证码发送中'); 
        	var ajax = new $ax("/newforgot/sms", 'get', function (res) {
        		UserLogin.gotuuid = res.data;
        		admin.btnLoading('#newForgotCode', false);
        		formX.startTimer('#newForgotCode', 30, function(time){
        			if(time == 1){
        				$('#newForgotCode').attr('disabled', false)
        			}
        			return time + 's 后可继续发送';
                });
            }, function (res) {
            	admin.btnLoading('#newForgotCode', false);
            	$('#newForgotCode').attr('disabled', false)
            	mynotice.error(res.msg);
            });
        	ajax.set('phone', phone);
            ajax.start();
        },
        seltab: () => { // 选择业态标签
        	admin.open({
                type: 1,
                title:  ['选择业态标签', 'font-size:24px;text-align:center;color: #ff4200;border-bottom: 0px solid #fff;'],
                area: '550px',
                move: false,
                content: '<div class="container"></div>',
                success: function (layero, dIndex) {
                	var ajax = new $ax("/api/format/list", 'get', function (res) {
                		var data = res.data;
                		var html = '';
                		$.each(data, (i, v) => {
                			html += '<div class="box" lay-event="formattag" data-id="'+ v.formatId +'" data-name="'+ v.formatName +'"><div class="img">'+ v.formatName +'</div></div>';
                		})
                        $('.container').html(html);
                		util.event('lay-event', {
                			formattag: function() {
                				UserLogin.formatId = $(this).data('id');
                				var name = $(this).data('name');
                				layer.close(dIndex);
                				$('#seltab').val(name);
                			}
                		});
                    }, function (res) {
                    	mynotice.error(res.msg);
                    });
                    ajax.start();
                	
                }
            });
        },
        home: () => { // 首页
        	location.replace('/');
        }
    });
    
    /**
     * 监听账号密码登录
     */
    form.on('submit(userLoginSubmit)', function(data){
    	var formData = data.field;
    	if(formData.username == '' || formData.username.length == 0){
    		$("#account").text('请输入手机号');
        	$("#account").fadeIn();
        	return false;
    	}
    	if(!UserLogin.pattern.test(formData.username)){
    		$("#account").text('手机号码格式不正确');
        	$("#account").fadeIn();
        	return false;
    	}
    	if(formData.password == '' || formData.password.length == 0){
    		$('#account').hide();
    		$("#accounterror").text('请输入密码');
        	$("#accounterror").fadeIn();
        	return false;
    	}else if(formData.password.length < 6){
    		$('#account').hide();
    		$("#accounterror").text('密码长度不够');
        	$("#accounterror").fadeIn();
        	return false;
    	}
    	mynotice.load('正在登录...');
    	$('#account').hide();
    	$('#accounterror').hide();
    	var ajax = new $ax("/userLogin", 'post', function (res) {
		    var remember = $("input[name='remember-me']").is(":checked"); // 是否记住密码
    		UserLogin.setCach(data.field, remember);
    		setter.putToken(res.data.token);
    		return location.replace('/');
        }, function (res) {
        	mynotice.destroy();
        	mynotice.errorc(res.msg);
        });
    	ajax.setData(JSON.stringify(formData));
        ajax.start();
    	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    
    /**
     * 监听手机验证码登录
     */
    form.on('submit(userPhoneLoginSubmit)', function(data){
    	var formData = data.field;
    	if(formData.username == '' || formData.username.length == 0){
    		$("#account").text('请输入手机号');
        	$("#account").fadeIn();
        	return false;
    	}
    	if(formData.code == '' || formData.code.length == 0){
    		$('#account').hide();
    		$("#pcodeerror").text('请输入验证码');
        	$("#pcodeerror").fadeIn();
        	return false;
    	}else if(formData.code.length != 4){
    		$('#account').hide();
    		$("#pcodeerror").text('请输入4四位验证码');
        	$("#pcodeerror").fadeIn();
        	return false;
    	}
    	$('#account').hide();
    	$('#pcodeerror').hide();
    	mynotice.load('正在登录...');
    	var ajax = new $ax("/userCodeLogin", 'post', function (res) {
    		var remember = $("input[name='remember-me2']").is(":checked"); // 是否记住密码
    		UserLogin.setCach(data.field, remember);
    		return location.replace('/');
        }, function (res) {
        	mynotice.destroy();
        	mynotice.error(res.msg);
        });
    	formData.uuid = UserLogin.uuid;
    	ajax.setData(JSON.stringify(formData));
        ajax.start();
    	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    
    /**
     * 注册
     */
    form.on('submit(userPhoneRegSubmit)', function(data){
    	var formData = data.field;
    	if(formData.username == '' || formData.username.length == 0){
    		$("#signupaccounterror").text('请输入手机号');
        	$("#signupaccounterror").fadeIn();
        	return false;
    	}
    	if(!UserLogin.pattern.test(formData.username)){
    		$("#signupaccounterror").text('手机号码格式不正确');
        	$("#signupaccounterror").fadeIn();
        	return false;
    	}
    	if(formData.code == '' || formData.code.length == 0){
    		$('#signupaccounterror').hide();
    		$("#pregcodeerror").text('请输入验证码');
        	$("#pregcodeerror").fadeIn();
        	return false;
    	}else if(formData.code.length != 4){
    		$('#signupaccounterror').hide();
    		$("#pregcodeerror").text('请输入4四位验证码');
        	$("#pregcodeerror").fadeIn();
        	return false;
    	}
    	if(formData.password == '' || formData.code.password == 0){
    		$('#signupaccounterror').hide();
    		$("#pregcodeerror").hide();
    		$("#signinpwd").text('请输入密码');
        	$("#signinpwd").fadeIn();
        	return false;
    	}else if(formData.password.length < 5 || formData.password.length > 16){
    		$('#signupaccounterror').hide();
    		$("#pregcodeerror").hide();
    		$("#signinpwd").text('请输入密码6-16位密码');
        	$("#signinpwd").fadeIn();
        	return false;
    	}
//    	if(UserLogin.formatId == ''){
//    		$('#signupaccounterror').hide();
//        	$('#pregcodeerror').hide();
//        	$("#signinpwd").hide();
//    		$("#laber").text('请选择业态标签');
//        	$("#laber").fadeIn();
//        	return false;
//    	}
    	if(formData.agree != 1){
    		$('#signupaccounterror').hide();
        	$('#pregcodeerror').hide();
        	$("#signinpwd").hide();
        	$("#laber").hide();
        	$("#agreeinfo").text('请同意隐私政策');
        	$("#agreeinfo").fadeIn();
        	return false;
    	}
    	
    	$('#signupaccounterror').hide();
    	$('#pregcodeerror').hide();
    	$("#signinpwd").hide();
    	$('#laber').hide();
    	$('#agreeinfo').hide();
    	mynotice.load('正在注册...');
    	var ajax = new $ax("/signup", 'post', function (res) {
            mynotice.destroy();
            mynotice.success('注册成功');
            $('#pcodeerror').hide();
        	$('#phoneSig-ccccc').hide();
            $("#registerWhole").hide();
            $("#passWhole").show();
            $("#phoneWhole").hide();
            $("[name='username']").val(formData.username);
            $("[name='password']").val('');
        }, function (res) {
        	mynotice.destroy();
        	mynotice.error(res.msg);
        });
    	formData.uuid = UserLogin.reguuid;
    	formData.formatId = UserLogin.formatId;
    	ajax.setData(JSON.stringify(formData));
        ajax.start();
    	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
   
    /**
     * 找回密码
     */
    form.on('submit(userNewforgotSubmit)', function(data){
    	var formData = data.field;
    	if(formData.phonenumber == '' || formData.phonenumber.length == 0){
    		$("#phoneSig-ccccc").text('请输入手机号');
        	$("#phoneSig-ccccc").fadeIn();
        	return false;
    	}
    	if(!UserLogin.pattern.test(formData.phonenumber)){
    		$("#phoneSig-ccccc").text('手机号码格式不正确');
        	$("#phoneSig-ccccc").fadeIn();
        	return false;
    	}
    	if(formData.code == '' || formData.code.length == 0){
    		$('#phoneSig-ccccc').hide();
    		$("#pregcodeerror").text('请输入验证码');
        	$("#pregcodeerror").fadeIn();
        	return false;
    	}else if(formData.code.length != 4){
    		$('#phoneSig-ccccc').hide();
    		$("#pcodeerror").text('请输入4四位验证码');
        	$("#pcodeerror").fadeIn();
        	return false;
    	}
    	if(formData.password == '' || formData.code.password == 0){
    		$('#phoneSig-ccccc').hide();
    		$("#pcodeerror").hide();
    		$("#newpassworderror").text('请输入密码');
        	$("#newpassworderror").fadeIn();
        	return false;
    	}else if(formData.newpassword.length < 5 || formData.newpassword.length > 16){
    		$('#phoneSig-ccccc').hide();
    		$("#pcodeerror").hide();
    		$("#newpassworderror").text('请输入密码6-16位密码');
        	$("#newpassworderror").fadeIn();
        	return false;
    	}
    	$('#phoneSig-ccccc').hide();
    	$('#pcodeerror').hide();
    	$("#newpassworderror").hide();
    	mynotice.load('正在处理...');
    	var ajax = new $ax("/user/newforgot", 'post', function (res) {
            mynotice.destroy();
            mynotice.success('更改成功');
            location.replace('/login');
        }, function (res) {
        	mynotice.destroy();
        	mynotice.error(res.msg);
        });
    	formData.uuid = UserLogin.gotuuid;
    	ajax.setData(JSON.stringify(formData));
        ajax.start();
    	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
    
});